package org.six11.olive3.picturephone;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;

import javax.swing.*;

import org.six11.olive3.client.OliveState;
import org.six11.olive3.net.MessageNames;
import org.six11.olive3.net.OliveNetworkTransmitter;
import org.six11.olive3.picturephone.server.PPMessageNames;
import org.six11.olive3.util.MultiState;
import org.six11.util2.Debug;

/**
 * 
 * 
 * @author Gabe Johnson <johnsogg@cmu.edu>
 * 
 */
public class PicturephoneConsole extends JDialog {

  private JTextArea serverMessages;

  public JTextArea getServerMessages() {
    return serverMessages;
  }

  private JTextArea clientCommands;
  // private PicturephoneDrawingPane drawingPane;
  // private OliveState state;
  // private MultiState programState;

  public PicturephoneConsole(/*final PicturephoneDrawingPane drawingPane_,*/
      final OliveNetworkTransmitter xmit, final MultiState programState) {
    super();
    // this.drawingPane = drawingPane_;
    setLayout(new BorderLayout());
    setSize(400, 300);
    Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
    Dimension frameSize = getSize();
    int x = (screenSize.width - frameSize.width) / 2;
    int y = (screenSize.height - frameSize.height) / 2;
    setLocation(x, y);
    serverMessages = new JTextArea();
    JScrollPane serverMessagesScroller = new JScrollPane(serverMessages);
    clientCommands = new JTextArea();
    JScrollPane clientCommandsScroller = new JScrollPane(clientCommands);
    JButton clientGoButton = new JButton("Send");
    JPanel bottomPanel = new JPanel();
    bottomPanel.setLayout(new BorderLayout());
    bottomPanel.add(clientCommandsScroller, BorderLayout.CENTER);
    bottomPanel.add(clientGoButton, BorderLayout.EAST);
    ActionListener sendAction = new ActionListener() {
      public void actionPerformed(ActionEvent ev) {
        String msg = clientCommands.getText();
        clientCommands.setText("");
        bug("Your message to the server will be: " + msg);
        String uid = programState.getString(MessageNames.PARAM_USER_ID);
        if (uid == null) {
          bug("Who are you again?");
        } else {
          String signature = PPMessageNames.MSG_CHAT + "(user=\"" + MessageNames.PARAM_USER_ID
              + uid + "\")";
          String statement = PPMessageNames.PARAM_MESSAGE + " \"" + msg + "\"";
          xmit.addToMessage(signature, statement);
          bug("Sent chat message '" + msg + "'");
        }
      }
    };
    clientGoButton.addActionListener(sendAction);
    JSplitPane splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT, serverMessagesScroller,
        bottomPanel);
    splitPane.setResizeWeight(0.8);
    add(splitPane, BorderLayout.CENTER);
    addWindowListener(new WindowAdapter() {
      public void windowActivated(WindowEvent e) {
        clientCommands.requestFocus();
      }

      public void windowDeiconified(WindowEvent e) {
        clientCommands.requestFocus();
      }

      public void windowOpened(WindowEvent e) {
        clientCommands.requestFocus();
      }
    });
  }

  private void bug(String what) {
    Debug.out("PicturephoneConsole", what);
  }

  /**
   * @param oliveString
   */
  public void addMessage(String oliveString) {
    serverMessages.append(oliveString);
    serverMessages.setCaretPosition(serverMessages.getDocument().getLength());
  }
}
